package com.icemelon.catdog.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.icemelon.catdog.dao.PetDetailsMapper;
import com.icemelon.catdog.dao.PetMapper;
import com.icemelon.catdog.pojo.Pet;
import com.icemelon.catdog.pojo.PetDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class PetServiceImpl implements PetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailsMapper petDetailsMapper;
    @Override
    public Pet findPetById(Integer id) {
        Pet pet = petMapper.selectById(id);
        return pet;
    }

    @Override
    public void savePet(Pet pet, PetDetails petDetails) {
       petMapper.insert(pet);
        petDetails.setId(pet.getId());
        petDetailsMapper.insert(petDetails);
    }

    @Override
    public void deletePets(Integer[] ids) {
        QueryWrapper<Pet> queryWrapper=new QueryWrapper<>();
       queryWrapper.in("id", ids);
        petMapper.delete(queryWrapper);
        //删除宠物详细信息
        List<Integer> idList= Arrays.asList(ids);
        petDetailsMapper.deleteBatchIds(idList);

    }

    @Override
    public void updatePet(Pet pet, PetDetails petDetails) {
        petMapper.updateById(pet);
        petDetails.setId(pet.getId());
        petDetailsMapper.updateById(petDetails);

    }

    @Override
    public PetDetails findPetByIds(Integer id) {
        return petDetailsMapper.selectById(id);
    }


}
